<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\SpecialTopic;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class SpecialTopicController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new SpecialTopic(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column("title");
            $grid->column("start_time","范围区间")->display(function (){
                return $this->start_time." - ".$this->end_time;
            });
            $grid->column('is_open')->switch();
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
            });
            // 禁用详情按钮
            $grid->disableViewButton();
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new SpecialTopic(), function (Show $show) {
            $show->field('id');
//            $show->html('content');
//            $show->content()->as(function ($content) {
//                return "<pre>{$content}</pre>";
//            });
//            $show->field('is_open');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new SpecialTopic(), function (Form $form) {
            $form->display('id');
            $form->text("title");
            $form->dateRange("start_time", "end_time", '展示时间区间')->rules("required",[
                "required"=>"展示时间区间必须"
            ]);
            $form->editor("content")->rules("required",[
                "required"=>"内容必须"
            ]);
            $form->switch("is_open")->default(1);
        });
    }
}
